package com.yixiu.study.juc.sync;

import java.util.concurrent.TimeUnit;

/**
 * @CalssName T02_sync
 * @Description
 * @Author yixiu
 * @Date 2021/6/24 9:37
 * @Verison num1.0
 **/
public class T02_sync {

    Object o = new Object();

    void m(){
        synchronized (o) {
            while (true){
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e){
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName());
            }
        }
    }

    public static void main(String[] args) {
        T02_sync t = new T02_sync();
        //启动第一个线程
        new Thread(t::m, "t1").start();

        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e){
            e.printStackTrace();
        }

        //启动第二个线程
        Thread t2 = new Thread(t::m, "t2");
        t.o = new Object();

        t2.start();

    }

}
